<?php
/**
 * The application bootstrap used by Zend_Application
 *
 * @category   Bootstrap
 * @package    Bootstrap
 * @copyright  Copyright (c) 2008 Keith Pope (http://www.thepopeisdead.com)
 * @license    http://www.thepopeisdead.com/license.txt     New BSD License
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    /**
     * @var Zend_Log
     */
    protected $_logger;
    
    /**
     * @var Zend_Application_Module_Autoloader
     */
    protected $_resourceLoader;
    
    /**
     * @var Zend_Controller_Front
     */
    public $frontController;
    
    /**
     * Setup the logging
     */
    protected function _initLogging()
    {
        
        $this->bootstrap('frontController');
        $logger = new Zend_Log();
        
        $writer = 'production' == $this->getEnvironment() ?
                new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../data/logs/app.log') :
                new Zend_Log_Writer_Firebug();
        
        $logger->addWriter($writer);
        
        if ('production' == $this->getEnvironment()) {
            $filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT);
            $logger->addFilter($filter);
        }
        
        $this->_logger = $logger;
        Zend_Registry::set('log', $logger);
        
    }


    /**
     * Configure the default modules autoloading, here we first create
     * a new module autoloader specifiying the base path and namespace
     * for our default module. This will automatically add the default
     * resource types for us. We also add two custom resources for Services
     * and Model Resources.
     */ 
    protected function _initDefaultModuleAutoloader()
    {
        
        $this->_logger->info('Bootstrap' . __METHOD__);
        
        $this->_resourceLoader = new 
                Zend_Application_Module_Autoloader(array(
                    'namespace' => 'Storefront',
                    'basePath' => APPLICATION_PATH . '/modules/storefront',
        ));
        
        
        $this->_resourceLoader->addResourceTypes(array(
            'modelResource' => array(
                'path'      => 'models/resources',
                'namespace' => 'Resource',
            ),
            'service' =>array(
                'path' => 'services',
                'namespace' => 'Service',
            ),
        ));
        
    }


    /**
     * Setup the database profiling
     */
    protected function _initDbProfiler()
    {
        
        $this->_logger->info('Bootstrap' . __METHOD__);
        
        if('production' !== $this->getEnvironment()) {
            $this->bootstrap('db');
            $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
            $profiler->setEnabled(TRUE);
            $this->getPluginResource('db')
                 ->getDbAdapter()
                 ->setProfiler($profiler);
        }
        
    }
    
    /**
     * Add the config to the registry
     */
    protected function _initConfig()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);
        Zend_Registry::set('config', $this->getOptions());
    }


    /**
     * Setup locale
     */
    protected function _initLocale()
    {
        
        $this->_logger->info('Bootstrap' . __METHOD__);
        $locale = new Zend_Locale('en_GB');
        Zend_Registry::set('Zend_Locale', $locale);
        
    }
    
    /**
     * Setup the view
     */
    protected function _initViewSettings()
    {
        $this->_logger->info('Bootstrap' . __METHOD__);
        
        // call the view resource plugin
        $this->bootstrap('view');
        
        $this->_view = $this->getResource('view');
        
        // set encoding and doctype
        $this->_view->setEncoding('UTF-8');
        $this->_view->doctype('XHTML1_STRICT');
        
        // set the content type and language
        $this->_view
             ->headMeta()
             ->setHttpEquiv('Content-Type', 'text/html; charset=utf-8') // fa sparire la A-circonflesso
             ->appendHttpEquiv('Content-Language', 'en-US');            // da fianco al simbolo £ o €
        
        // set css links
        $this->_view
             ->headStyle()
             ->setStyle('@import "/css/access.css";');
        $this->_view
             ->headLink()
             ->appendStylesheet('/css/reset.css');
        $this->_view
             ->headLink()
             ->appendStylesheet('/css/main.css');
        $this->_view
             ->headLink()
             ->appendStylesheet('/css/form.css');
        
        // set the title
        $this->_view
             ->headTitle('Storefront');
        
        // set separator string for segments
        $this->_view
             ->headTitle()
             ->setSeparator(' - ');
        
    }
    
    protected function _initRoutes()
    {
        
        $this->_logger->info('Bootstrap' . __METHOD__);
        
        $this->bootstrap('frontController');
        
        $router = $this->frontController->getRouter();
        
        // catalog category product route
        $route = new Zend_Controller_Router_Route(
                'catalog/:categoryIdent/:productIdent', 
                array(
                    'action'        => 'view',
                    'controller'    => 'catalog',
                    'module'        => 'storefront',
                    'categoryIdent' => '',
                ), 
                array(
                    'categoryIdent' => '[a-zA-Z-_0-9]+',
                    'productIdent'  => '[a-zA-Z-_0-9]+',
                )
            );
        $router->addRoute('catalog_category_product', $route);
        
        // catalog category route
        $route = new Zend_Controller_Router_Route(
                'catalog/:categoryIdent/:page', 
                array(
                    'action'        => 'index',
                    'controller'    => 'catalog',
                    'module'        => 'storefront',
                    'categoryIdent' => '',
                    'page'          => 1,
                ), 
                array(
                    'categoryIdent' => '[a-zA-Z-_0-9]+',
                    'page'  => '\d+',
                )
            );
        $router->addRoute('catalog_category', $route);
    }
    

}

